package org.eiahe.hr.performance.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.constant.RoleKeyConstants;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.common.satoken.utils.LoginHelper;
import org.ehe.system.domain.HrEmployee;
import org.ehe.system.service.IHrEmployeeService;
import org.eiahe.hr.performance.domain.PerformanceRewardPunish;
import org.eiahe.hr.performance.domain.PerformanceScore;
import org.eiahe.hr.performance.domain.bo.PerformanceRewardPunishBo;
import org.eiahe.hr.performance.domain.vo.PerformanceRewardPunishVo;
import org.eiahe.hr.filler.EmployeeNameFiller;
import org.eiahe.hr.performance.mapper.PerformanceRewardPunishMapper;
import org.eiahe.hr.performance.service.IPerformanceRewardPunishService;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 奖惩记录Service业务层处理
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class PerformanceRewardPunishServiceImpl implements IPerformanceRewardPunishService {

    private final PerformanceRewardPunishMapper baseMapper;
    private final IHrEmployeeService employeeService;
    private final EmployeeNameFiller employeeNameFiller;

    @Override
    public PerformanceRewardPunishVo queryById(Long recordId) {
        PerformanceRewardPunishVo performanceRewardPunishVo = baseMapper.selectVoById(recordId);
        employeeNameFiller.fillEmployeeName(
            performanceRewardPunishVo,
            PerformanceRewardPunishVo::getEmployeeNo,
            PerformanceRewardPunishVo::setEmployeeName);
        return performanceRewardPunishVo;
    }

    private LambdaQueryWrapper<PerformanceRewardPunish> buildQueryWrapper(PerformanceRewardPunishBo record) {
        LambdaQueryWrapper<PerformanceRewardPunish> lqw = Wrappers.lambdaQuery();
        if (!(StpUtil.hasRoleOr(RoleKeyConstants.HR_ROLES.toArray(new String[0])))) {
            // 获取当前登录用户名
            HrEmployee hrEmployee = employeeService.queryByUserId(LoginHelper.getUserId());
            if(Objects.nonNull(hrEmployee)){
                // 添加创建者过滤条件
                lqw.like(PerformanceRewardPunish::getEmployeeNo, hrEmployee.getEmployeeNo());
            }
        }

        if (StringUtils.isNotBlank(record.getEmployeeName())) {
            List<String> employeeNos = employeeService.queryByEmployeeName(record.getEmployeeName());
            if (CollUtil.isEmpty(employeeNos)) {
                lqw.eq(PerformanceRewardPunish::getEmployeeNo, -1);
            } else {
                lqw.in(PerformanceRewardPunish::getEmployeeNo, employeeNos);
            }
        }
        lqw.eq(StringUtils.isNotBlank(record.getEmployeeNo()), PerformanceRewardPunish::getEmployeeNo, record.getEmployeeNo());
        lqw.eq(StringUtils.isNotBlank(record.getRecordType()), PerformanceRewardPunish::getRecordType, record.getRecordType());
        lqw.like(StringUtils.isNotBlank(record.getReason()), PerformanceRewardPunish::getReason, record.getReason());
        return lqw;
    }

    @Override
    public TableDataInfo<PerformanceRewardPunishVo> queryPageListVo(PerformanceRewardPunishBo record,
            PageQuery pageQuery) {
        LambdaQueryWrapper<PerformanceRewardPunish> lqw = buildQueryWrapper(record);
        Page<PerformanceRewardPunishVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        employeeNameFiller.fillEmployeeNames(
            page.getRecords(),
            PerformanceRewardPunishVo::getEmployeeNo,
            PerformanceRewardPunishVo::setEmployeeName);
        return TableDataInfo.build(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(PerformanceRewardPunishBo record) {
        PerformanceRewardPunish add = MapstructUtils.convert(record, PerformanceRewardPunish.class);
        return baseMapper.insert(add) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(PerformanceRewardPunishBo record) {
        PerformanceRewardPunish update = MapstructUtils.convert(record, PerformanceRewardPunish.class);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        if (isValid) {
            // 校验逻辑可以根据需要添加
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
